/**
* author: SamWu
* email：samwulqy@gmail.com
**/
<?php
/**
 * 生成安全的随机字符串
 */
function generateSecureRandom($length = 32) {
    try {
        return bin2hex(random_bytes($length / 2));
    } catch (Exception $e) {
        return md5(uniqid(mt_rand(), true));
    }
}

/**
 * 这里计算方差
 */
function calculateVariance($array) {
    if (count($array) < 2) return 0;
    
    $mean = array_sum($array) / count($array);
    $sum = 0;
    
    foreach ($array as $value) {
        $sum += pow($value - $mean, 2);
    }
    
    return $sum / count($array);
}

/**
 * 计算位置方差
 */
function calculatePositionVariance($positions) {
    $xValues = array_column($positions, 'x');
    $yValues = array_column($positions, 'y');
    
    $xVariance = calculateVariance($xValues);
    $yVariance = calculateVariance($yValues);
    
    return ($xVariance + $yVariance) / 2;
}

/**
 * 计算模式方差
 */
function calculatePatternVariance($movements) {
    $timings = [];
    $distances = [];
    
    for ($i = 1; $i < count($movements); $i++) {
        $prev = $movements[$i-1];
        $curr = $movements[$i];
        
        $dx = $curr['x'] - $prev['x'];
        $dy = $curr['y'] - $prev['y'];
        $distance = sqrt($dx*$dx + $dy*$dy);
        $time = $curr['timestamp'] - $prev['timestamp'];
        
        $timings[] = $time;
        $distances[] = $distance;
    }
    
    $timingVariance = calculateVariance($timings);
    $distanceVariance = calculateVariance($distances);
    
    return ($timingVariance + $distanceVariance) / 2;
}

/**
 * 分析其他行为指标
 */
function analyzeBehaviorIndicators($data) {
    $score = 50;
    
    // 检查用户代理
    if (isset($data['user_agent'])) {
        if (isSuspiciousUserAgent($data['user_agent'])) {
            $score -= 20;
        } elseif (isCommonBrowser($data['user_agent'])) {
            $score += 10;
        }
    }
    
    // 检查屏幕分辨率
    if (isset($data['screen_resolution'])) {
        if ($data['screen_resolution'] === '0x0' || 
            $data['screen_resolution'] === 'undefined') {
            $score -= 15;
        } elseif (isCommonResolution($data['screen_resolution'])) {
            $score += 5;
        }
    }
    
    // 检查时区
    if (isset($data['timezone'])) {
        if (isSuspiciousTimezone($data['timezone'])) {
            $score -= 10;
        }
    }
    
    // 检查插件列表
    if (isset($data['plugins'])) {
        if (empty($data['plugins']) || count($data['plugins']) < 2) {
            $score -= 5;
        }
    }
    
    return max(0, min(100, $score));
}

/**
 * 检查可疑用户代理
 */
function isSuspiciousUserAgent($ua) {
    $suspicious = [
        'curl', 'wget', 'python', 'java', 'php', 'go-http', 'scrapy',
        'headlesschrome', 'phantomjs', 'bot', 'spider', 'crawler'
    ];
    
    foreach ($suspicious as $pattern) {
        if (stripos($ua, $pattern) !== false) {
            return true;
        }
    }
    
    return false;
}

/**
 * 检查常见浏览器
 */
function isCommonBrowser($ua) {
    $common = [
        'chrome', 'firefox', 'safari', 'edge', 'opera', 'msie', 'trident'
    ];
    
    foreach ($common as $pattern) {
        if (stripos($ua, $pattern) !== false) {
            return true;
        }
    }
    
    return false;
}

/**
 * 检查常见分辨率
 */
function isCommonResolution($res) {
    $common = [
        '1920x1080', '1366x768', '1440x900', '1600x900', 
        '1280x800', '1280x1024', '1024x768'
    ];
    
    return in_array($res, $common);
}

/**
 * 检查可疑时区
 */
function isSuspiciousTimezone($tz) {
    return $tz === 'UTC' || $tz === 'GMT';
}
?>
